package com.yztc.orangeentertainment.adapter;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.channalbean.ListBean;
import com.yztc.orangeentertainment.bean.staritembean.ContentsBean;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/9.
 * 资讯adapter
 */
public class StarNewsInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private View view;
    private Context context;
    private boolean flag;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    private OnItemClickListener listener;


    public void setListBean(List<ListBean> listBean) {
        this.listBean = listBean;
    }

    private List<ListBean> listBean;
    public static final int TYPE_NORMAL = 0;
    public static final int ITEM_TYPE_GIF = 1;
    public static final int ITEM_TYPE_VIDEO = 2;
    public static final int ITEM_TYPE_TUJI = 8;

    public StarNewsInfoAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_GIF) {
            //type=1对应的view
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channal_content_rvitem4, parent, false);
            return new MyGifViewHolder(view);
        } else if (viewType == ITEM_TYPE_VIDEO) {
            //type=2对应的view
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channal_content_rvitem3, parent, false);
            return new MyVideoViewHolder(view);
        } else if (viewType == ITEM_TYPE_TUJI) {
            //type=8对应的view
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_starnews_tuji, parent, false);
            return new MyTujiViewHolder(view);
        } else {
            //type=0对应的view
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channal_content_rvitem1, parent, false);
            return new MyNormalViewHolder(view);
        }


    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
        //normal
        if (viewHolder instanceof MyNormalViewHolder) {
            Glide.with(context).load(listBean.get(position).getPic()).into(((MyNormalViewHolder) viewHolder)
                    .ivChannalrvitem1photo);
            ((MyNormalViewHolder) viewHolder).tvChannalrvitem1title.setText(listBean.get(position).getTitle());
            if (listBean.get(position).getCat() != null && !"".equals(listBean.get(position).getCat())) {
                ((MyNormalViewHolder) viewHolder).dvChannalrvitem1icon.setImageURI(listBean.get(position).getCat()
                        .getIcon());
                ((MyNormalViewHolder) viewHolder).tvChannalrvitem1type.setText(listBean
                        .get(position).getCat().getName());
            }
            ((MyNormalViewHolder) viewHolder).tvChannalrvitem1scanner.setText(listBean.get(position).getReadnum() + "");

        }
        //video
        if (viewHolder instanceof MyVideoViewHolder) {
            Glide.with(context).load(listBean.get(position).getPic()).into(((MyVideoViewHolder) viewHolder).ivSuoluotu);
            ((MyVideoViewHolder) viewHolder).ivSuoluotu.setVisibility(View.VISIBLE);
            ((MyVideoViewHolder) viewHolder).vvChannalrvitem3.setVideoURI(Uri.parse(listBean.get(position).getSrc()));
            ((MyVideoViewHolder) viewHolder).ivChannalrvplay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MyVideoViewHolder) viewHolder).ivSuoluotu.setVisibility(View.GONE);
                    //开始播放视频
                    if (flag == false) {
                        flag = true;
                        ((MyVideoViewHolder) viewHolder).vvChannalrvitem3.start();
                        ((MyVideoViewHolder) viewHolder).ivChannalrvplay.setImageResource(R.mipmap.ic_feed_pause);
                        ((MyVideoViewHolder) viewHolder).vvChannalrvitem3.setMediaController(new MediaController(context));
                    } else {
                        flag = false;
                        ((MyVideoViewHolder) viewHolder).ivChannalrvplay.setImageResource(R.mipmap
                                .ic_feed_play);
                        ((MyVideoViewHolder) viewHolder).vvChannalrvitem3.pause();
                        ((MyVideoViewHolder) viewHolder).vvChannalrvitem3.setMediaController(new MediaController(context));
                    }
                }
            });
            if (listBean.get(position).getCat() != null && !"".equals(listBean.get(position).getCat())) {
                ((MyVideoViewHolder) viewHolder).ivChannalrvitem3icon.setImageURI(listBean.get(position).getCat().getIcon());
                ((MyVideoViewHolder) viewHolder).tvChannalrvitem3icon.setText(listBean.get(position).getCat().getName());
            }
            ((MyVideoViewHolder) viewHolder).tvChannalrvitem3title.setText(listBean.get(position).getTitle());
            ((MyVideoViewHolder) viewHolder).tvChannalrvitem3scanner1.setText(listBean.get(position).getReadnum() + "");
        }
        //gif
        if (viewHolder instanceof MyGifViewHolder) {
            Glide.with(context).load(listBean.get(position).getGif().get(0).getUrl()).into(((MyGifViewHolder) viewHolder).gvChannalrvitem4);
            ((MyGifViewHolder) viewHolder).tvChannalrvitem4title.setText(listBean.get(position).getTitle());
            ((MyGifViewHolder) viewHolder).tvChannalrvitem4name.setText(listBean.get(position).getAuthor().getName());
        }
        //recycleview
        if (viewHolder instanceof MyTujiViewHolder) {
            ((MyTujiViewHolder) viewHolder).tvTujiTitle.setText(listBean.get(position).getTitle());
            ((MyTujiViewHolder) viewHolder).dvTujiIcon.setImageURI(listBean.get(position).getCat().getIcon());
            ((MyTujiViewHolder) viewHolder).tvTujiType.setText(listBean.get(position).getCat().getName());
            ((MyTujiViewHolder) viewHolder).tvTujiScanner.setText(listBean.get(position).getReadnum()+"");
            List<ContentsBean> list = listBean.get(position).getContentsBeanList();
            if (list != null) {
                NewsPicAdapter adapter = new NewsPicAdapter(list, context);
                ((MyTujiViewHolder) viewHolder).mRecyclerView.setAdapter(adapter);
            }
        }
        if (listener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = viewHolder.getLayoutPosition();
                    listener.onItemClick(viewHolder.itemView, pos);
                }
            });

            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = viewHolder.getLayoutPosition();
                    listener.onItemLongClick(viewHolder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return listBean.size();
    }

    @Override
    public int getItemViewType(int position) {

        if (listBean.get(position).getType() == 8) {
            return ITEM_TYPE_TUJI;
        } else if (listBean.get(position).getType() == 2) {
            return ITEM_TYPE_VIDEO;
        } else if (listBean.get(position).getType() == 1) {
            return ITEM_TYPE_GIF;
        } else {
            return TYPE_NORMAL;
        }
    }

    public class Holder extends RecyclerView.ViewHolder {
        public Holder(View itemView) {
            super(itemView);
        }
    }


    //normal
    class MyNormalViewHolder extends Holder {
        @InjectView(R.id.tv_channalrvitem1title)
        TextView tvChannalrvitem1title;
        @InjectView(R.id.dv_channalrvitem1icon)
        SimpleDraweeView dvChannalrvitem1icon;
        @InjectView(R.id.tv_channalrvitem1type)
        TextView tvChannalrvitem1type;
        @InjectView(R.id.iv_channalrvitem1scanner)
        ImageView ivChannalrvitem1scanner;
        @InjectView(R.id.tv_channalrvitem1scanner)
        TextView tvChannalrvitem1scanner;
        @InjectView(R.id.iv_channalrvitem1delete)
        ImageView ivChannalrvitem1delete;
        @InjectView(R.id.iv_channalrvitem1photo)
        ImageView ivChannalrvitem1photo;

        public MyNormalViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
            tvChannalrvitem1scanner = (TextView) itemView.findViewById(R.id.tv_channalrvitem1scanner);
        }
    }

    //video
    class MyVideoViewHolder extends Holder {
        @InjectView(R.id.vv_channalrvitem3)
        VideoView vvChannalrvitem3;
        @InjectView(R.id.iv_channalrvplay)
        ImageView ivChannalrvplay;
        @InjectView(R.id.tv_channalrvitem3title)
        TextView tvChannalrvitem3title;
        @InjectView(R.id.iv_channalrvitem3icon)
        SimpleDraweeView ivChannalrvitem3icon;
        @InjectView(R.id.tv_channalrvitem3icon)
        TextView tvChannalrvitem3icon;
        @InjectView(R.id.iv_channalrvitem3scanner)
        ImageView ivChannalrvitem3scanner;
        @InjectView(R.id.tv_channalrvitem3scanner1)
        TextView tvChannalrvitem3scanner1;
        @InjectView(R.id.iv_channalrvitem3delete)
        ImageView ivChannalrvitem3delete;
        @InjectView(R.id.iv_suoluotu)
        ImageView ivSuoluotu;

        public MyVideoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    //gif
    class MyGifViewHolder extends Holder {
        @InjectView(R.id.gv_channalrvitem4)
        ImageView gvChannalrvitem4;
        @InjectView(R.id.tv_channalrvitem4title)
        TextView tvChannalrvitem4title;
        @InjectView(R.id.tv_channalrvitem4name)
        TextView tvChannalrvitem4name;
        @InjectView(R.id.iv_channalrvitem4share)
        Button ivChannalrvitem4share;

        public MyGifViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
        }
    }

    class MyTujiViewHolder extends Holder {
        @InjectView(R.id.rv_tuji)
        RecyclerView mRecyclerView;
        @InjectView(R.id.tv_tuji_title)
        TextView tvTujiTitle;
        @InjectView(R.id.dv_tuji_icon)
        SimpleDraweeView dvTujiIcon;
        @InjectView(R.id.tv_tuji_type)
        TextView tvTujiType;
        @InjectView(R.id.iv_tuji_scanner)
        ImageView ivTujiScanner;
        @InjectView(R.id.tv_tuji_scanner)
        TextView tvTujiScanner;

        public MyTujiViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            //水平摆放
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            //间距
            mRecyclerView.addItemDecoration(new DividerItemDecoration(5));
            mRecyclerView.setLayoutManager(linearLayoutManager);
        }
    }


}
